package socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 服务端
 */
public class Server {
    private ServerSocket serverSocket;

    /**
     * 服务端构造器
     */
    public Server() {
        System.out.println("正在启动服务端");
        try {
            serverSocket = new ServerSocket(6606);

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("服务端启动完毕");
    }

    public void start() {
        try {
            System.out.println("等待连接...");
            Socket socket = serverSocket.accept();
            System.out.println("一个客户端已连接");
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            socket.getInputStream(), StandardCharsets.UTF_8));
            String s;
            while ((s = br.readLine()) != null) {
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
}
